#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

run_recovery_from_source() {
    desc "aws restore with backup source"

    stext=$(kubectl_bin get pxc-backup on-demand-backup-aws-s3 -o json | jq -c '.status')
    jq --argjson s "$stext" '.spec.backupSource=$s' $test_dir/conf/restore-from-source-backup-aws-s3.json \
        | kubectl_bin apply -f -
    sleep 30
    wait_for_running "$cluster-proxysql" 1
    wait_for_running "$cluster-pxc" 3
    sleep 30
    desc 'check data after backup from source'
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-0.$cluster-pxc -uroot -proot_password"
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-1.$cluster-pxc -uroot -proot_password"
    compare_mysql_cmd "select-1" "SELECT * from myApp.myApp;" "-h $cluster-pxc-2.$cluster-pxc -uroot -proot_password"

}

main() {
    create_infra $namespace
    start_minio

    cluster="demand-backup"
    spinup_pxc "$cluster" "$test_dir/conf/$cluster.yml"

    run_backup         "$cluster" "on-demand-backup-pvc"
    run_recovery_check "$cluster" "on-demand-backup-pvc"
    check_pvc_md5

    run_backup         "$cluster" "on-demand-backup-pvc-bsource"
    run_recovery_check "$cluster" "on-demand-backup-pvc-bsource"

    if [ -z "$SKIP_BACKUPS_TO_AWS_GCP" ]; then
        run_backup         "$cluster" "on-demand-backup-aws-s3"
        run_recovery_check "$cluster" "on-demand-backup-aws-s3"

        run_recovery_from_source

        run_backup         "$cluster" "on-demand-backup-gcp-cs"
        run_recovery_check "$cluster" "on-demand-backup-gcp-cs"
    fi

    run_backup         "$cluster" "on-demand-backup-minio"
    run_recovery_check "$cluster" "on-demand-backup-minio"

    destroy $namespace
}

main
